<template>
	<view class="container">
		<view class="page-body">
			<view class='wrapper'>
				<view class='toolbar' @tap="format" style="height: 140rpx;overflow-y: auto;">
					<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
					</view>
					<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic">
					</view>
					<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
						data-name="underline"></view>
					<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian"
						data-name="strike"></view>
					<!-- #ifndef MP-BAIDU -->
					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
						data-name="align" data-value="left"></view>
					<!-- #endif -->
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
						data-name="align" data-value="center"></view>
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
						data-name="align" data-value="right"></view>
					<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
						data-name="align" data-value="justify"></view>
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
						data-name="list" data-value="ordered"></view>
					<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
						data-name="list" data-value="bullet"></view>
					<view class="iconfont icon-undo" @tap="undo"></view>
					<view class="iconfont icon-redo" @tap="redo"></view>
					<view class="iconfont icon-charutupian" @tap="insertImage"></view>
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
						data-name="header" :data-value="1"></view>
					<view class="iconfont icon-shanchu" @tap="clear"></view>
				</view>

				<view class="editor-wrapper">
					<editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar
						show-img-resize @statuschange="onStatusChange" :read-only="readOnly" :adjust-position="false"
						@ready="onEditorReady" @input="change">
					</editor>
				</view>
				<!-- <view class="wap btm" @tap="finish">
					<button class="btn">完成</button>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				readOnly: false,
				formats: {},
				content: ''
			}
		},
		created() {
			// this.content = uni.getStorageSync('editor') || ''
		},
		methods: {
			readOnlyChange() {
				this.readOnly = !this.readOnly
			},
			onEditorReady() {
				// #ifdef MP-BAIDU
				this.editorCtx = requireDynamicLib('editorLib').createEditorContext('editor');
				// #endif

				// #ifdef APP-PLUS || MP-WEIXIN || H5
				uni.createSelectorQuery()
				.in(this)
				.select('#editor')
				.fields(
					{
						size: true, //是否返回节点尺寸（width height）
						context: true //是否返回节点尺寸（width height）
					},
					(res) => {
						// console.log('----------',res);
						this.editorCtx = res.context
						this.editorCtx.setContents({
							html: this.content //this.EditGoodsDetail.content为赋值内容。    
						})
					}
				)
				.exec();
				// #endif
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.editorCtx.format(name, value)
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
				// console.log(formats)
			},
			clear() {
				uni.showModal({
					title: '清空编辑器',
					content: '确定清空编辑器全部内容？',
					success: res => {
						if (res.confirm) {
							this.editorCtx.clear({
								success: function(res) {
									// console.log("clear success")
								}
							})
						}
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				let that = this
				uni.chooseImage({
					count: 1,
					success: async (res) => {
						// console.log(res)
						const tempFiles = res.tempFiles;
						let url = await that.$upload(tempFiles[0].path,()=>{})
						// console.log('-------',url)
						that.editorCtx.insertImage({
							src: url,
							alt: '图像',
							success: function() {
								// console.log('insert image success')
							}
						})
					}
				})
			},
			finish() {
				// uni.setStorageSync("editor", this.content)
				uni.navigateBack({
					delta: 1
				})
			},
			change(e) {
				if (e.target.html) {
					this.content = e.target.html
				}


			}
		}
	}
</script>

<style lang="scss">
	@import '@/components/editor/editor-icon.css';
	.btm{
		position: fixed;
		width: 100%;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		background: #fff;
		bottom: 0;
		left: 0;
		width: 750rpx;
		display: flex;
		align-items: center;
		button{
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
	.page-body {
		// min-height: calc(100vh - var(--window-top) - var(--status-bar-height));
	}

	.wrapper {
		height: 100%;
	}

	.editor-wrapper {
		margin-top: -36rpx;
		// min-height: calc(100vh - var(--window-top) - var(--status-bar-height) - 120px);
		min-height: 300rpx;
		background: #fff;
	}

	.iconfont {
		width: 70rpx;
		height: 70rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;
		font-size: 20px;
	}

	.toolbar {
		display: flex;
		flex-wrap: wrap;
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
		// position: fixed;
		top: 0;
		background: #eee;
		z-index: 30;
	}

	.ql-container {
		
		box-sizing: border-box;
		padding: 12px 15px;
		width: 100%;
		min-height: 30vh;
		height: 100%;
		margin-top: 20px;
		font-size: 16px;
		line-height: 1.5;
	}

	.ql-active {
		color: #06c;
	}

	.btn {
		width: 100%;
		padding: 20rpx 30rpx;
	}
</style>
